package com.ruoyi.device.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.device.domain.DeviceBorrow;
import com.ruoyi.device.domain.DeviceInfo;
import com.ruoyi.device.domain.QueryDevice;
import com.ruoyi.device.domain.QueryUser;
import com.ruoyi.device.service.IDeviceBorrowService;
import com.ruoyi.device.service.IDeviceInfoService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 设备类别Controller
 * @auth
 * @date
 */
@RestController
@RequestMapping("/device/deviceBorrow")
public class DeviceBorrowController extends BaseController {
    @Autowired
    private IDeviceInfoService deviceService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IDeviceBorrowService borrowService;

    /**
     * 查询借用信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DeviceBorrow params){
        //检测查询参数用户名是否为空（注意string引用的是org.apacho.commons.lang3包裹的类）
        if(StringUtils.isNoneBlank(params.getUserName())){
            //通过用户名获取用户信息
            SysUser queryUser = userService.getUserByName(params.getUserName());
            //如果用户信息为空，则设置查询参数userid
            if(queryUser != null){
                params.setUserId(queryUser.getUserId());
            }
        }
        startPage();
        List<DeviceBorrow> list = borrowService.getBorrowList(params);
        return getDataTable(list);
    }

    /**
     * 新增快速借出功能，自动补全借用人信息是，按名字获取借用人列表
     */
    @GetMapping("/queryUser/{nickName}")
    public AjaxResult queryUserList(@PathVariable String nickName){
        //新建查询cans（实体类）
        SysUser query = new SysUser();
        query.setNickName(nickName);

        //按查询条件查询用户信息
        List<SysUser> list = userService.selectUserList(query);

        //把查询出的用户列表转为，自动补全实体类
        List<QueryUser> ret = new ArrayList<>();
        for(SysUser u: list){
            ret.add(new QueryUser(u.getUserId(), u.getUserName(), u.getNickName()));
        }
        return success(ret);
    }
    /**
     * 查询设备列表
     * @params deviceName
     * @return
     */
    @GetMapping("/queryDevice/{deviceName}")
    public AjaxResult queryDevcieList(@PathVariable String deviceName){
        //新建查询参数（实体类）
        DeviceInfo device = new DeviceInfo();
        device.setDeviceName(deviceName);

        //按查询条件查询设备信息
        List<DeviceInfo> list = deviceService.getDeviceList(device);

        //把查询出的用户列表转为，自动补全实体类
        List<QueryDevice> ret = new ArrayList<>();
        for(DeviceInfo d: list) {
            ret.add(new QueryDevice(d.getDeviceId(), d.getDeviceName(), d.getDeviceModel()));
        }
        return success(ret);
    }
    /**
     * 导出借用信息列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response , DeviceBorrow borrow){
        List<DeviceBorrow> list = borrowService.getBorrowList(borrow);
        ExcelUtil<DeviceBorrow> util = new ExcelUtil<>(DeviceBorrow.class);
        util.exportExcel(response, list,"借用信息数据");
    }

    /**
     * 获取借用信息详细信息
     * @param id
     * @return
     */
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        return success(borrowService.getBorrowById(id));
    }

    /**
     * 新增借用信息
     * @param borrow
     * @return
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody DeviceBorrow borrow){
        return toAjax(borrowService.addBorrow(borrow));
    }

    /**
     * 修改借用信息
     * @param borrow
     * @return
     */
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody DeviceBorrow borrow){
        return toAjax(borrowService.editBorrow(borrow));
    }

    /**
     * 删除设备信息
     * @param ids
     * @return
     */
    @DeleteMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable("ids") Long[] ids) {
        return toAjax(borrowService.removeBorrowByIds(ids));
    }
}
